<?php

namespace addons\sum\controller\performer;

use addons\sum\controller\Common;
use app\admin\model\sum\performer\Performer as PerformerModel;
use app\admin\model\sum\performer\Follow as PerformerFollowModel;
use think\Db;

class Performer extends Common
{

    protected $noNeedLogin = ['index','detail','recommend'];
    protected $noNeedRight = ['*'];

    /**
     * 演员列表
     */
    public function index()
    {
        $user = auth_user();
        $list_rows = $this->request->param('list_rows', 20);
        $sort = $this->request->param('sort','weigh');
        $order = $this->request->param('order','desc');
        $is_recommend = $this->request->param('is_recommend');
        $is_follow = $this->request->param('is_follow');

        $query=new PerformerModel();
        $query->field('*,(popularity + fake_popularity) AS total_popularity,(followers + fake_followers) AS total_followers');
        if($is_recommend==1){
            $query=$query->where('is_recommend',1);
        }
        if($is_follow==1&&$user){
            $query=$query->whereExists(function ($q)use($user){
                $self_name = (new PerformerModel())->getQuery()->getTable();
                $item_name = (new PerformerFollowModel())->getQuery()->getTable();
                $q->table($item_name)->where('performer_id=' . $self_name . '.id')
                    ->where('user_id', $user['id']);
            });
        }
        $query=$query->where('status','normal');
        $records=$query->order('weigh DESC')->paginate($list_rows);
        $records->each(function ($item)use($user){
            $item['is_follow']=0;
            if($user){
                $follow=PerformerFollowModel::where('performer_id',$item['id'])->where('user_id',$user['id'])->find();
                if($follow){
                    $item['is_follow']=1;
                }
            }
        });
        $this->success(__('获取成功'),$records);
    }


    /**
     * 关注演员
     * @return void
     */
    public function follow(){
        $user = auth_user();
        $performer_id = $this->request->param('performer_id');
        if(!$performer_id){
            $this->error(__('参数错误'));
        }
        // 演员
        $performer=PerformerModel::where('id',$performer_id)->where('status','normal')->find();
        if(!$performer){
            $this->error(__('该演员不存在'));
        }
        $follow=PerformerFollowModel::where('performer_id',$performer['id'])->where('user_id',$user['id'])->find();
        if($follow){
            $performer->followers=$performer->followers-1;
            $performer->save();
            $follow->delete();
        }else{
            $performer->followers=$performer->followers+1;
            $performer->save();
            $follow=new PerformerFollowModel();
            $follow->user_id=$user['id'];
            $follow->user_nickname=$user['nickname'];
            $follow->user_avatar=$user['avatar'];
            $follow->user_gender=$user['gender'];
            $follow->performer_id=$performer['id'];
            $follow->save();
        }
        $this->success(__('操作成功'));
    }

    /**
     * 演员详情 - 标签 - 贡献排行
     * @return void
     */
    public function detail(){
        $user = auth_user();
        $performer_id = $this->request->param('performer_id');
        if(!$performer_id){
            $this->error(__('参数错误'));
        }
        // 演员
        $performer=PerformerModel::field('*,(popularity + fake_popularity) AS total_popularity,(followers + fake_followers) AS total_followers')->where('id',$performer_id)->where('status','normal')->find();
        if(!$performer){
            $this->error(__('该演员不存在'));
        }
        $return=[];
        $return['performer']=$performer;
        $return['user']=$user;
        $followers=PerformerFollowModel::where('performer_id',$performer['id'])->where('popularity','>',0)->order('popularity DESC')->limit(20)->select();
        $return['followers']=$followers;
        $return['user_ranking']=0;
        if($user){
            $user_follow=PerformerFollowModel::where('performer_id',$performer['id'])->where('user_id',$user['id'])->find();
            if($user_follow){
                $gt_user_count=PerformerFollowModel::where('performer_id',$performer['id'])->where('popularity','>',$user_follow['popularity'])->count();
                $return['user_ranking']=$gt_user_count+1;
            }else{
                $return['user_ranking']=0;
            }
        }
        // 抽卡消耗
        $card_score=sum_config('shop.basic.card_score');
        $return['card_score']=$card_score;
        $this->success(__('获取成功'),$return);
    }
    /**
     * 贴标签
     * @return void
     */
    public function tag()
    {
        $user = auth_user();
        $performer_id = $this->request->param('performer_id');
        $tag = $this->request->param('tag');
        if (!$performer_id) {
            $this->error(__('参数错误'));
        }
        if (!$tag) {
            $this->error(__('参数错误'));
        }
        // 演员
        $performer = PerformerModel::where('id', $performer_id)->where('status', 'normal')->find();
        if (!$performer) {
            $this->error(__('该演员不存在'));
        }
        $tags=$performer['tags'];
        $tags[]=$tag;
        $performer->tags=$tags;
        $performer->save();
        $this->success(__('操作成功'));
    }
}
